package com.sicent.operate.common.util;

/** 
 * @author:zhaoyinchuan
 *
 * @version:1
 *
 * @Create:2014年6月4日 下午12:53:10 
 */
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import org.springframework.web.context.ContextLoader;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QREncoder {
	
	public static void generate(String content, String name){
		generate(content, "c:/QREncoder", name, "png", 200);
	}
	
	public static void generate(String content, String path, String name){
		generate(content, path, name, "png", 200);
	}
	
	public static void generate(String content, String path, String name, String imgType){
		generate(content, path, name, imgType, 200);
	}
	
	public static void generate(String content, String path, String name, String imgType, int size){
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
//		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 
		hints.put(EncodeHintType.MARGIN, 1); 
		BitMatrix matrix = null;
		try {
			MultiFormatWriter writer = new MultiFormatWriter();
			matrix = writer.encode(content, BarcodeFormat.QR_CODE, size, size,hints);
//			matrix = deleteWhite(matrix);
		} catch (WriterException e) {
			e.printStackTrace();
		}

		File file = new File(path, name + "." + imgType);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		try {
			MatrixToImageWriter.writeToFile(matrix, imgType, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void generateToStream(String content, OutputStream out, String imgType, int size){
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		hints.put(EncodeHintType.MARGIN, 1); 
		BitMatrix matrix = null;
		try {
			MultiFormatWriter writer = new MultiFormatWriter();
			matrix = writer.encode(content, BarcodeFormat.QR_CODE, size, size,hints);
			//matrix = deleteWhite(matrix);
		} catch (WriterException e) {
			e.printStackTrace();
		}
		try {
			MatrixToImageWriter.writeToStream(matrix, imgType, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void generateToFile(String content, OutputStream out, String imgType, int size){
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 
		hints.put(EncodeHintType.MARGIN, 1); 
		BitMatrix matrix = null;
		try {
			MultiFormatWriter writer = new MultiFormatWriter();
			matrix = writer.encode(content, BarcodeFormat.QR_CODE, size, size,hints);
			matrix = deleteWhite(matrix);
		} catch (WriterException e) {
			e.printStackTrace();
		}
		try {
			String relativeUploadPath =  "/upload/QR/aa.jpg";
			String uploadPath = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath(relativeUploadPath);
			File saveDir = new File(uploadPath);
			if(!saveDir.exists()){
				saveDir.mkdirs();
			}
			System.out.println("file.getAbsolutePath()..."+saveDir.getAbsolutePath());
			MatrixToImageWriter.writeToFile(matrix, imgType, saveDir);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//去白边
	public static BitMatrix deleteWhite(BitMatrix matrix){
		int[] rec = matrix.getEnclosingRectangle();
		int resWidth = rec[2] + 1;
		int resHeight = rec[3] + 1;

		BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
		resMatrix.clear();
		for (int i = 0; i < resWidth; i++) {
			for (int j = 0; j < resHeight; j++) {
				if (matrix.get(i + rec[0], j + rec[1]))
					resMatrix.set(i, j);
			}
		}
		return resMatrix;
	}
}
